<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>专业信息</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../css/public.css" media="all" />
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<!-- create_time: "2021-04-22 21:34:41"
description: "相当牛逼的一个专业"
establish_time: "2019-10-01 10:00:00"
full_name: "自动控制"
id: 7
school: {id: 1, create_time: "2021-04-22 21:07:12", update_time: "2021-04-22 21:07:58", title: "机械学院",…}
short_name: "自控"
sketch: "自动控制专业"
update_time: "2021-04- -->

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <input type="text" name="id" hidden />
            <label class="layui-form-label required">专业名称</label>
            <div class="layui-input-inline">
                <input type="text" name="full_name" lay-verify="required" lay-reqtext="专业名称不能为空" placeholder="请输入专业名称"
                    value="" class="layui-input" />
                <!-- <tip>填写学院编号。</tip> -->
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">专业概述</label>
            <div class="layui-input-inline">
                <input type="text" name="sketch" placeholder="请输入专业的简单描述" value="" class="layui-input" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">专业详细说明</label>
            <div class="layui-input-inline">
                <textarea name="description" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">专业创立时间</label>
            <div class="layui-input-inline">
                <input id="establish_time" type="text" name="establish_time" placeholder="请选择时间" value=""
                    class="layui-input" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">所属学院</label>
            <div class="layui-input-inline">
                <select name="dp" id="dp" lay-verify="required" value="" lay-filter="xmFilter">
                    <!-- <option value=""></option> -->
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">
                    确认保存
                </button>
            </div>
        </div>
    </div>
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(["form", "laydate"], function () {
            var form = layui.form,
                layer = layui.layer,
                laydate = layui.laydate,
                $ = layui.$;
            //执行一个laydate实例
            laydate.render({
                elem: "#establish_time", //指定元素
                type: 'datetime'
            });
            form.render()
            $.ajax({
                url: 'http://localhost:8080/school/list',
                dataType: 'json',
                type: 'get',
                success: function (data) {
                    // console.log(data); //下面会提到这个data是什么值
                    //使用循环遍历，给下拉列表赋值
                    $.each(data, function (index, value) {
                        $('#dp').append(new Option(value.title, value.id)); // 下拉菜单里添加元素
                    });
                    form.render('select'); //重新渲染 固定写法
                }
            })

            //监听提交
            form.on("submit(saveBtn)", function (data) {
                // console.log(data.field);
                // console.log(JSON.stringify(data.field))
                var school = {
                    id: data.field.dp
                }
                // console.log($('#dp').val())
                // console.log(school)
                data.field.school = school


                $.ajax({
                    contentType: "application/json",
                    url: "http://localhost:8080/major/save",
                    type: "POST",
                    dataType: "JSON",
                    data: JSON.stringify(data.field),
                    success: function (result) {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    },
                    error: function (xhr, status, p3, p4) {
                        var err = "Error " + " " + status + " " + p3;
                        if (xhr.responseText && xhr.responseText[0] == "{")
                            err = JSON.parse(xhr.responseText).message;
                        alert(err);
                    },
                });
                return false;
            });
        });
    </script>
</body>

</html>